using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Dynamic;
using System.Data.SqlClient;
using System.Configuration;
using Model;
using Common;

namespace DAL
{
    public class HistoryDAL
    {
        public static int Save(HistoryInfo info)
        {
            string sql = string.Format("delete from [history] where AdminID=@AdminID and ProductID=@ProductID and UserID=@UserID;insert into [history]([AdminID],[ProductID],[UserID],[CreateDate]) values(@AdminID,@ProductID,@UserID,@CreateDate);select @@identity;");
            SqlParameter[] paras = new SqlParameter[]{
                new SqlParameter("@AdminID", info.AdminID),
                new SqlParameter("@ProductID", info.ProductID),
                new SqlParameter("@UserID", info.UserID),
                new SqlParameter("@CreateDate", info.CreateDate)
            };
            return DataConvert.ToInt(CommonDAL.ExecuteScalar(sql, paras));
        }
        public static int Update(HistoryInfo info)
        {
            string sql = string.Format("update [history] set [AdminID]=@AdminID,[ProductID]=@ProductID where [ID]=@ID");
            SqlParameter[] paras = new SqlParameter[]{
                new SqlParameter("@AdminID", info.AdminID),
                new SqlParameter("@ProductID", info.ProductID),
                new SqlParameter("@ID", info.ID)
            };
            return CommonDAL.ExecuteNoneQuery(sql, paras) ? info.ID : 0;
        }
        public static HistoryInfo FindOneByID(int id)
        {
            DataRow dr = CommonDAL.FindOneByID(id, "[history]");
            return SimpleData(dr);
        }
        public static List<HistoryInfo> FindListByIDs(string ids)
        {
            DataTable dt = CommonDAL.FindListByIDs(ids, "[history]");
            return ListSimpleData(dt);
        }
        public static List<HistoryInfo> FindPage(int userid, string keyword, string begindate, string enddate, int startindex, int pagesize, string order, string desc, out int count)
        {
            StringBuilder filter = new StringBuilder();
            filter.AppendFormat(" and [history].UserID ={0}", userid);
            if (!string.IsNullOrEmpty(keyword))
            {
                filter.AppendFormat(" and [history].Name like '%{0}%'", keyword);
            }
            if (!string.IsNullOrEmpty(begindate) && !string.IsNullOrEmpty(enddate))
            {
                filter.AppendFormat(" and [history].CreateDate between '{0}' and '{1}'", DataConvert.ToDateTime(begindate).ToString("yyyy-MM-dd 00:00:00"), DataConvert.ToDateTime(enddate).ToString("yyyy-MM-dd 23:59:59"));
            }
            DataTable dt = CommonDAL.FindCPage(startindex, pagesize, order, desc, filter, "[history_page]", out count);
            return ListAllData(dt);
        }
        public static List<HistoryInfo> FindAll()
        {
            DataTable dt = CommonDAL.GetDataTable(string.Format("select * from [history]"));
            return ListSimpleData(dt);
        }
        private static HistoryInfo SimpleData(DataRow dr)
        {
            HistoryInfo info = new HistoryInfo();
            if (dr != null)
            {
                info.ID = DataConvert.ToInt(dr["ID"]);
                info.AdminID = DataConvert.ToInt(dr["AdminID"]);
                info.ProductID = DataConvert.ToInt(dr["ProductID"]);
                info.UserID = DataConvert.ToInt(dr["UserID"]);
                info.CreateDate = DataConvert.ToDateTime(dr["CreateDate"]);
                info.CreateText = info.CreateDate.ToString("yyyy-MM-dd");
            }
            return info;
        }
        private static HistoryInfo AllData(DataRow dr)
        {
            HistoryInfo info = new HistoryInfo();
            if (dr != null)
            {
                info.ID = DataConvert.ToInt(dr["ID"]);
                info.AdminID = DataConvert.ToInt(dr["AdminID"]);
                info.ProductID = DataConvert.ToInt(dr["ProductID"]);
                info.UserID = DataConvert.ToInt(dr["UserID"]);
                info.CreateDate = DataConvert.ToDateTime(dr["CreateDate"]);
                info.CreateText = info.CreateDate.ToString("yyyy-MM-dd");

                info.Name = DataConvert.ToString(dr["Name"]);
                info.Img = (Commons.domain + DataConvert.ToString(dr["Imgs"]).Replace(",", "," + Commons.domain)).Split(',')[0];
                info.Unit = DataConvert.ToString(dr["Unit"]);
                info.OPriceText = DataConvert.ToFloat(dr["OPrice"]).ToString("0.00");
                info.APriceText = DataConvert.ToFloat(dr["APrice"]).ToString("0.00").Split('.');
                info.AllSellCount = DataConvert.ToInt(dr["AllSellCount"]);
            }
            return info;
        }
        private static List<HistoryInfo> ListSimpleData(DataTable dt)
        {
            List<HistoryInfo> list = new List<HistoryInfo>();
            foreach (DataRow row in dt.Rows)
            {
                list.Add(SimpleData(row));
            }
            return list;
        }
        private static List<HistoryInfo> ListAllData(DataTable dt)
        {
            List<HistoryInfo> list = new List<HistoryInfo>();
            foreach (DataRow row in dt.Rows)
            {
                list.Add(AllData(row));
            }
            return list;
        }
    }
}

